/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file    usart.c
 * @brief   This file provides code for the configuration
 *          of the USART instances.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2024 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "usart.h"

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

UART_HandleTypeDef huart1;

/* USART1 init function */

void MX_USART1_UART_Init(void)
{

  /* USER CODE BEGIN USART1_Init 0 */

  /* USER CODE END USART1_Init 0 */

  /* USER CODE BEGIN USART1_Init 1 */

  /* USER CODE END USART1_Init 1 */
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */

  /* USER CODE END USART1_Init 2 */

}

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(uartHandle->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspInit 0 */

  /* USER CODE END USART1_MspInit 0 */
    /* USART1 clock enable */
    __HAL_RCC_USART1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART1 GPIO Configuration
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* USART1 interrupt Init */
    HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspInit 1 */

  /* USER CODE END USART1_MspInit 1 */
  }
}

void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{

  if(uartHandle->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspDeInit 0 */

  /* USER CODE END USART1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART1_CLK_DISABLE();

    /**USART1 GPIO Configuration
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);

    /* USART1 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspDeInit 1 */

  /* USER CODE END USART1_MspDeInit 1 */
  }
}

/* USER CODE BEGIN 1 */

static UART_HandleTypeDef *sg_hUart;

void UART_ReDirect(UART_HandleTypeDef *huart)
{
    sg_hUart = huart;

    setvbuf(stdout, NULL, _IONBF, 0);
    setvbuf(stdin, NULL, _IONBF, 0);
}

PUTCHAR_PROTOTYPE
{
    HAL_UART_Transmit(sg_hUart, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
    return ch;
}

GETCHAR_PROTOTYPE
{
    uint8_t ch = 0;
    HAL_UART_Receive(sg_hUart, &ch, 1, HAL_MAX_DELAY);
    return (int)ch;
}

// 串口中断接收部分
#include <string.h>

// 串口中断接收单个字符
static char Serial_RxChar;
static char Serial_RxStrBuffer[SERIAL_RX_BUFFER_SIZE];
static volatile bool Serial_ReadLineReadyFlag;

// 初始化函数
void Serial_Init(void)
{
    Serial_ReadLineReadyFlag = false;
    memset((void *)Serial_RxStrBuffer, 0, SERIAL_RX_BUFFER_SIZE);
    HAL_UART_Receive_IT(&huart1, (uint8_t *)&Serial_RxChar, 1);
}

Serial_StatueTypeDef Serial_GetStatus(void)
{
    Serial_StatueTypeDef result = SERIAL_NOT_READY;
    __disable_irq(); // 关中断
    if (Serial_ReadLineReadyFlag == true) {
        Serial_ReadLineReadyFlag = false;
        result                   = SERIAL_RECV_LINE_READY;
    }
    __enable_irq(); // 开中断
    return result;
}

void Serial_GetRxStr(char *dest)
{
    __disable_irq();
    strncpy(dest, Serial_RxStrBuffer, SERIAL_RX_BUFFER_SIZE);
    Serial_ClearRxStr();
    __enable_irq();
}

void Serial_ClearRxStr(void)
{
    __disable_irq();
    memset(Serial_RxStrBuffer, 0, SERIAL_RX_BUFFER_SIZE);
    __enable_irq();
}

static void ProcessRxChar()
{
    static __IO uint16_t index = 0;
    switch (Serial_RxChar) {
        case '\r':
            break;
        case '\n':
            Serial_ReadLineReadyFlag  = true;
            Serial_RxStrBuffer[index] = '\0';
            index                     = 0;
            break;
        default:
            if (index < SERIAL_RX_BUFFER_SIZE - 1) { // 预留空间给'\0'
                Serial_RxStrBuffer[index] = Serial_RxChar;
                index++;
            } else {
                index = 0; // 防止越界
            }
            break;
    }
}

// 串口中断接收中断回调函数
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if (huart->Instance == USART1) {
        ProcessRxChar();
        HAL_UART_Receive_IT(&huart1, (uint8_t *)&Serial_RxChar, 1);
    }
}

/* USER CODE END 1 */
